﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundService2Ref;
    using WMS6.Web.UserControls;

    public class VanningList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F7541;
        protected GridView gvVanning;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblVanning;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2VanningList(VanningQueryEntity vanningQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(vanningQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundService2ClientProxy.GetInstance().GetVanningInfoByQueryEntity(vanningQuery);
            base.BindData2GridView(this.gvVanning, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvVanning, "VANNING_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请至少选择一项进行删除操作";
            }
            else
            {
                try
                {
                    this.lblMsg.Text = "删除装箱单号：" + OutboundService2ClientProxy.GetInstance().DeleteVanning(selectedID) + "成功!";
                    VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
                    this.BindData2VanningList(vanningQuery);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvVanning, "VANNING_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项进行编辑";
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = "只能选择一项进行编辑";
            }
            else
            {
                base.Response.Redirect(string.Format("Vanning.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
            vanningQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = vanningQuery.CurrentPage;
            vanningQuery.CurrentPage = "0";
            this.gvVanning.Columns[1].Visible = false;
            this.gvVanning.AllowSorting = false;
            this.BindData2VanningList(vanningQuery);
            this.hdnActionID.Value = "";
            this.gvVanning.ShowFooter = false;
            vanningQuery.IsGetAll = false;
            vanningQuery.CurrentPage = currentPageIndex;
            this.SetVanningQueryEntity(vanningQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Vanning.xls", this.gvVanning);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
            vanningQuery.CurrentPage = "0";
            this.BindData2VanningList(vanningQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
            vanningQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2VanningList(vanningQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
            vanningQuery.VanningID = "";
            vanningQuery.OrderID = "";
            vanningQuery.OrderType = "";
            vanningQuery.Status = "";
            vanningQuery.VanningDate = "";
            vanningQuery.Remark = "";
            this.BindData2VanningList(vanningQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
            if (int.Parse(vanningQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                vanningQuery.CurrentPage = (int.Parse(vanningQuery.CurrentPage) + 1).ToString();
                this.BindData2VanningList(vanningQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
            if (int.Parse(vanningQuery.CurrentPage) > 0)
            {
                vanningQuery.CurrentPage = (int.Parse(vanningQuery.CurrentPage) - 1).ToString();
                this.BindData2VanningList(vanningQuery);
            }
        }

        private VanningQueryEntity GetVanningQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as VanningQueryEntity);
            }
            return (base.PageQueryEntitySession as VanningQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
                vanningQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2VanningList(vanningQuery);
            }
        }

        protected void gvVanning_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7541", this.gvVanning);
        }

        protected void gvVanning_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
            }
        }

        protected void gvVanning_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                VanningQueryEntity vanningQuery = this.GetVanningQueryEntity();
                vanningQuery.SortField = e.SortExpression;
                if (vanningQuery.SortDirection == "ASC")
                {
                    vanningQuery.SortDirection = "DESC";
                }
                else if (vanningQuery.SortDirection == "DESC")
                {
                    vanningQuery.SortDirection = "ASC";
                }
                this.BindData2VanningList(vanningQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            VanningQueryEntity vanningQuery = new VanningQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    vanningQuery.VanningID = this.PreviousPage.VanningID;
                    vanningQuery.OrderID = this.PreviousPage.OrderID;
                    vanningQuery.OrderType = this.PreviousPage.OrderType;
                    vanningQuery.VanningDate = this.PreviousPage.VanningDate;
                    vanningQuery.Remark = this.PreviousPage.Remark;
                }
                else
                {
                    vanningQuery.VanningID = "";
                    vanningQuery.OrderID = "";
                    vanningQuery.OrderType = "";
                    vanningQuery.Status = "";
                    vanningQuery.VanningDate = "";
                    vanningQuery.Remark = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            vanningQuery.IsGetAll = false;
            vanningQuery.CurrentPage = "0";
            vanningQuery.PageSize = this.gvVanning.PageSize.ToString();
            vanningQuery.SortField = "VANNING_ID";
            vanningQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvVanning.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetVanningQueryEntity(vanningQuery);
            this.BindData2VanningList(vanningQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetVanningQueryEntity(VanningQueryEntity vanningQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = vanningQuery;
            }
            else
            {
                base.PageQueryEntitySession = vanningQuery;
            }
        }

        public VanningQuery PreviousPage
        {
            get
            {
                return (VanningQuery) base.PreviousPage;
            }
        }
    }
}

